package com.cskaoyan.market.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.service.AdminRegionService;
import com.cskaoyan.market.db.admin.vo.RegionInnerRegionVo;
import com.cskaoyan.market.db.admin.vo.RegionVo;
import com.cskaoyan.market.db.domain.MarketRegion;
import com.cskaoyan.market.db.domain.MarketRegionExample;
import com.cskaoyan.market.db.mapper.MarketRegionMapper;
import com.cskaoyan.market.util.MarketRegionConstant;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageInfo;

/**
 * @author 小焦
 * @since 2023/03/18 17:07
 */
public class AdminRegionServiceImpl implements AdminRegionService {

    /**
     * 思路： 1.所有的省市县数据交织在一起，不便于后续的处理，所以我们希望将省、市、县的数据分开 将这些数据分开有两种方式： 1根据type查询数据，可以分别获取省、市、县的数据
     * 2,获取全部数据之后，我们利用java的集合相关API自行去处理 如果我们使用方案二：将这些数据放置在map中，key是type，value值是list<Region> 2.将市、县的数据按照pid来进行分组
     * 3.循环遍历省的时候，利用map来获取当前省下面的city信息；遍历市的时候，需要利用map来获取当前市下面的area信息 4.按照要求，封装vo即可
     * 
     * @return
     */
    @Override
    public RegionVo list() {
        SqlSession session = MyBatisUtil.getSession();
        MarketRegionMapper regionMapper = session.getMapper(MarketRegionMapper.class);
        List<MarketRegion> marketRegions = regionMapper.selectByExample(new MarketRegionExample());
        PageInfo<MarketRegion> pageInfo = new PageInfo<>(marketRegions);
        session.commit();
        session.close();
        Map<Byte, List<MarketRegion>> RegionListByType = groupByType(marketRegions);
        List<MarketRegion> provinceList = RegionListByType.get(MarketRegionConstant.MARKET_TYPE_PROVINCE);
        List<MarketRegion> cityList = RegionListByType.get(MarketRegionConstant.MARKET_TYPE_CITY);
        List<MarketRegion> areaList = RegionListByType.get(MarketRegionConstant.MARKET_TYPE_AREA);
        Map<Integer, List<MarketRegion>> CityListGroup = groupByPid(cityList);
        Map<Integer, List<MarketRegion>> areaListGroup = groupByPid(areaList);

        List<RegionInnerRegionVo> regionVos = new ArrayList<>();
        for (MarketRegion province : provinceList) {
            RegionInnerRegionVo region = new RegionInnerRegionVo();
            region.setId(province.getId());
            region.setName(province.getName());
            region.setType(province.getType());
            region.setCode(province.getCode());
            List<MarketRegion> cities = CityListGroup.get(province.getId());
            List<RegionInnerRegionVo> regionVoCities = new ArrayList<>();
            for (MarketRegion city : cities) {
                RegionInnerRegionVo regionVoCity = new RegionInnerRegionVo();
                regionVoCity.setId(city.getId());
                regionVoCity.setName(city.getName());
                regionVoCity.setType(city.getType());
                regionVoCity.setCode(city.getCode());
                List<MarketRegion> areas = areaListGroup.get(regionVoCity.getId());
                List<RegionInnerRegionVo> regionVoAreas = new ArrayList<>();
                for (MarketRegion area : areas) {
                    RegionInnerRegionVo regionVoArea = new RegionInnerRegionVo();
                    regionVoArea.setId(area.getId());
                    regionVoArea.setName(area.getName());
                    regionVoArea.setType(area.getType());
                    regionVoArea.setCode(area.getCode());
                    regionVoAreas.add(regionVoArea);
                }
                regionVoCity.setChildren(regionVoAreas);
                regionVoCities.add(regionVoCity);
            }
            region.setChildren(regionVoCities);
            regionVos.add(region);

        }
        RegionVo regionVo = new RegionVo();
        regionVo.setTotal(provinceList.size());
        regionVo.setPages(pageInfo.getPages());
        regionVo.setLimit(provinceList.size());
        regionVo.setPage(1);
        regionVo.setList(regionVos);

        return regionVo;
    }

    /**
     * 根据父id 进行分类
     * 
     * @param regionList
     * @return
     */
    private Map<Integer, List<MarketRegion>> groupByPid(List<MarketRegion> regionList) {
        Map<Integer, List<MarketRegion>> result = new HashMap<>();
        // 遍历 regions 集合
        for (MarketRegion region : regionList) {
            // 根据type类型分类，1,2,3将list加以分类
            // 通过类型获取regionList
            List<MarketRegion> regions = result.get(region.getPid());
            // 如果regionList == null
            if (regions == null) {
                // 新建一个list
                regions = new ArrayList<>();
                // 添加进来
                result.put(region.getPid(), regions);
            }
            // 将这个Region对象添加进来
            regions.add(region);
        }
        return result;
    }

    /**
     * 根据省、市、县类型进行分组
     * 
     * @param marketRegions
     * @return
     */
    private Map<Byte, List<MarketRegion>> groupByType(List<MarketRegion> marketRegions) {

        Map<Byte, List<MarketRegion>> result = new HashMap<>();
        // 遍历 regions 集合
        for (MarketRegion marketRegion : marketRegions) {
            // 根据type类型分类，1,2,3将list加以分类
            // 通过类型获取regionList
            Byte type = marketRegion.getType();

            List<MarketRegion> regionList = result.get(type);
            // 如果regionList == null
            if (regionList == null) {
                // 新建一个list
                regionList = new ArrayList<>();
                // 添加进来
                result.put(marketRegion.getType(), regionList);
            }
            // 将这个Region对象添加进来
            regionList.add(marketRegion);
        }
        return result;
    }
}
